/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<double> averageOfLevels(TreeNode* root) {
        vector<double> ans;
        TreeNode* tmp;
        queue<TreeNode*> que;
        que.push(root);
        while(!que.empty()){
            int len=que.size();
            long long int sum=0;
            for(int i=0;i<len;i++){
                tmp=que.front();que.pop();
                sum+=tmp->val;
                if(tmp->left){que.push(tmp->left);}
                if(tmp->right){que.push(tmp->right);}
            }
            ans.push_back((double)sum/len);
        }
        return ans;
    }
};